<?php
namespace app\common\model;

use think\Model;

class Accounts extends Model
{
    //汽车结算列表
    public function getcaraccountslist($where,$page,$limit)
    {
        $list = db('contractcar')
            ->alias('c')
            ->field('c.contractcar_id,ca.car_plate,ca.car_name,(s.store_name) store,c.mileage,c.travel_time,(st.store_name) wherestore,c.contractcar_company,c.contractcar_bigontime,c.contractcar_endtime')
            ->join('car ca',"ca.car_id = c.car_id")
            ->join('store s',"s.store_id = ca.store_id","left")
            ->join('store st',"st.store_id = c.store_id","left")
            ->page($page, $limit)
            ->where($where)
            ->select();
        $count = db('contractcar')
            ->alias('c')
            ->join('car ca',"ca.car_id = c.car_id")
            ->join('store s',"s.store_id = ca.store_id","left")
            ->join('store st',"st.store_id = c.store_id","left")
            ->where($where)
            ->count();
        return ['count' => $count, 'data' => $list];
    }
    //驾驶员结算列表
    public function getdriveaccountslist($where,$page,$limit)
    {
        $list = db('contractcar')
            ->alias('c')
            ->field('c.contractcar_id,d.drive_name,d.drive_sex,d.drive_tel,c.mileage,c.travel_time,s.store_name,c.contractcar_company,c.contractcar_bigontime,c.contractcar_endtime')
            ->join('drive d',"d.drive_id = c.drive_id")
            ->join('store s',"s.store_id = d.store_id","left")
            ->page($page, $limit)
            ->where($where)
            ->select();
        $count = db('contractcar')
            ->alias('c')
            ->join('car ca',"ca.car_id = c.car_id")
            ->page($page, $limit)
            ->where($where)
            ->count();
        return ['count' => $count, 'data' => $list];
    }
    //汽车结算记录
    public function getcaraccountsinfo($where,$page,$limit)
    {
        //id，用车单位，用车时间，结算类型，用车时间段，用车时间，用车公里数，结算总金额，出租分公司，生成时间，
        $list = db('contractcar')
            ->alias('c')
            ->field('c.contractcar_id,c.contractcar_company,c.travel_time,from_unixtime(c.contractcar_bigontime,\'%Y .%m .%d %H:%i:%s\') contractcar_bigontime
            ,from_unixtime(c.contractcar_endtime,\'%Y .%m .%d %H:%i:%s\') contractcar_endtime,c.travel_time,c.mileage,
            st.store_name,from_unixtime(c.create_time,\'%Y .%m .%d %H:%i:%s\') create_time,va.*,ca.*,c.contractcar_what')
            ->join('car ca',"ca.car_id = c.car_id","left")
            ->join('vehicle_rent va',"ca.cartype_ids = va.cartype_id")
            ->join('store st',"st.store_id = c.store_id","left","left")
            ->page($page, $limit)
            ->where($where)
            ->select();
        //结算总金额
        foreach ($list as $k=>$v){
            $money = db('vehicle_rent')->where('cartype_id',$list[$k]['cartype_ids'])->find();
            if ($money){
                if ($list[$k]['contractcar_what'] == 1){
                    $list[$k]['allmoney'] = ($list[$k]['travel_time'] * $money['vehicle_priceday']) / 100;
                }else{
                    $list[$k]['allmoney'] = ($list[$k]['mileage'] * $money['vehicle_price']) / 100;
                }
            }else{
                $list[$k]['allmoney'] = "暂无";
            }

            $list[$k]['time'] = $list[$k]['contractcar_bigontime'] ."~~". $list[$k]['contractcar_endtime'];
        }
        $count = db('contractcar')
            ->alias('c')
            ->field('c.contractcar_id,c.contractcar_company,c.travel_time,from_unixtime(c.contractcar_bigontime,\'%Y .%m .%d %H:%i:%s\') contractcar_bigontime
            ,from_unixtime(c.contractcar_endtime,\'%Y .%m .%d %H:%i:%s\') contractcar_endtime,c.travel_time,
            c.mileage,st.store_name,from_unixtime(c.create_time,\'%Y .%m .%d %H:%i:%s\') create_time,va.*,ca.*')
            ->join('car ca',"ca.car_id = c.car_id","left")
            ->join('vehicle_rent va',"ca.cartype_ids = va.cartype_id")
            ->join('store st',"st.store_id = c.store_id","left","left")
            ->where($where)
            ->count();
        return ['count' => $count, 'data' => $list];
    }
}